<template>
	<view>
		<view>
			<view class="bg-gray" style="left: 0;right:0;z-index: 1;">
				<billInput ref="refBill" optType="RECEIVE" v-model="bill" @confirm="onBillKeyEnter"></billInput>
		
				<todoInput v-model="todoNum" @confirm="onConfirmTodo" :focus="todoFocus" @focusChange="onTodoFocusChange"></todoInput>
				
				<view class="cu-bar bg-white solid-bottom margin-top  shadow-blur">
					<view class="action">
						<text class="cuIcon-title text-orange"></text>
						物料列表
					</view>
				</view>
			</view>
			<!-- <view style="height:360rpx"></view> -->
			<scroll-view scroll-y="true" style="position: absolute;bottom:0;top:360rpx;bottom:100rpx">
				<view class="cu-list todoList menu">
					<view v-for="(item, idx) in todoList" class="cu-item" :class="item.act_qty == item.plan_qty ? 'finish' : ''">
						<view class="content padding-tb-sm">
							<view class="liaohao">
								<text class="cuIcon-activityfill text-orange margin-right-xs"></text>
								<text>单号:</text>
								{{ item.bill_id }}
							</view>
							<view class="liaohao">
								<text class="cuIcon-formfill text-blue margin-right-xs"></text>
								<text>料号:</text>
								{{ item.part_id }}
							</view>
							<view class="text-gray text-sm">
								<text class="lg text-gray cuIcon-settingsfill"></text>
								<text>来源:</text>
								{{ item.source_bill }}
							</view>
						</view>
						<view class="action">
							<view class="text-grey text-xs">
								<text class="text-gray">已理:</text>
								{{ item.act_qty }}
							</view>
							<view class="text-gray  text-xs">
								<text class="text-gray">应理:</text>
								{{ item.plan_qty }}
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
			<view class="cu-bar tabbar bg-white shadow foot foot-btn-group">
				<button class="cu-btn bg-orange" @tap="onFinish">理货完成</button>
				<button class="cu-btn bg-white" @tap="onOpenCancelModal">取消理货</button>
				<button class="cu-btn bg-white" @tap="onChangeBill">切换单号</button>
			</view>
			<view style="height:100rpx"></view>
		</view>
		<bill-prompt ref="refCancelPrompt" @confirm="onCancelConfirm"  title="取消理货"></bill-prompt>
	</view>
</template>

<script>
import api from '@/serveices/wms-api.js';
import billInput from './bill-input.vue';
import billPrompt from './bill-prompt.vue';
import todoInput from './todo-input.vue';
import util from '@/serveices/util.js';
const newId = (function() {
	var id = Date.parse(new Date()) * 1000;
	return () => id++;
})();
export default {
	components: { billInput, billPrompt,todoInput },
	data() {
		return {
			bill: '',
			todoNum: '',
			todoFocus:true,
			todoList: []
		};
	},
	methods: {
		onTodoFocusChange(focus){
			this.todoFocus = focus
		},
		onBillKeyEnter({ bill,extData }) {
			this.checkBillAndGetToDoList(bill,extData);
		},
		checkBillAndGetToDoList(outBill,ext) {
			var bill = outBill || this.bill;
			api.billdetail({
				bigType: 1,
				opType: 'RECEIVE',
				billId: bill
			})
				.then(r => {
					this.$refs.refBill.lock();
					this.todoNum = '';
					this.todoList = r.data;
					if(ext&&ext.playVoice){
						//来自todo的扫码
						util.playSuccess();
					}
					console.log(
						r.data.map(x => {
							return `P${x.part_id}|K${x.source_bill}|M1|D1|L1|Q10|S${newId()}`;
						})
					);
					this.todoFocus = true
				})
				.catch(e => {
					this.todoList = [];
					this.$refs.refBill.clear();
					this.todoNum=''
					if(ext&&ext.playVoice){
						//来自todo的扫码
						util.playFailed()
					}
				});
		},
		//单据切换
		onChangeBill() {
			this.$refs.refBill.clear();
			//setValueAndTriggerEnter
			this.todoList = [];
			this.todoNum = '';
			this.todoFocus = true
		},
		onConfirmTodo() {
			// 料号为空 直接跳过
			if (!this.todoNum) return;
			if (!this.bill) {
				//单号为空，此处当做单号
				this.$refs.refBill.setValueAndTriggerEnter(this.todoNum,null,null,{playVoice:true});
			} else {
				if (this.todoNum) {
					api.receive(this.bill, this.todoNum)
						.then(res => {
							this.todoList = res.data;
							this.todoNum = '';
							util.playSuccess();
						})
						.catch(err => {
							util.playFailed();
							this.todoNum = '';
						});
				}
			}
		},

		onFinish() {
			if (this.todoList && this.todoList.length) {
				var isOk = true;
				this.todoList.forEach(row => {
					if (row.act_qty !== row.plan_qty) {
						isOk = false;
					}
				});
				if (!isOk) {
					uni.showToast({
						icon: 'none',
						title: '理货未完成，无法进行理货完成。',
						duration: 2000
					});
				} else {
					//todo
					let _this = this;
					uni.showModal({
						title: '系统提示',
						content: '是否确认理货完成？',
						success: res => {
							if (res.confirm) {
								uni.showLoading({
									mask: true
									// title:'正在进行理货完成操作，请稍后...'
								});
								let p = Promise.resolve();
								_this.todoList.forEach(r => {
									p = p.then(t => {
										return api.receive(_this.bill, r.part_id + '|' + r.source_bill, 'CLOSED');
									});
								});
								p.then(r => {
									// 全部成功
									_this.onChangeBill();
								}).finally(() => {
									uni.hideLoading();
								});

								// Promise.all()
							} else if (res.cancel) {
							}
						}
					});
				}
			}
		},

		onOpenCancelModal() {
			this.$refs.refCancelPrompt.show();
		},
		onCancelConfirm(scan) {
			if (!scan) return;
			// if (!this.bill) {
			// 	//单号为空，此处当做单号
			// 	this.$refs.refBill.setValueAndTriggerEnter(scan);
			// } else {
				
			// }
			api.receive(this.bill, scan, 'CANCEL').then(res => {
				util.playSuccess()
				if(this.bill){
					this.checkBillAndGetToDoList(this.bill);
				}
			}).catch(err=>{
				util.playFailed()
			})
		}
	},
	beforeMount() {}
};
</script>

<style lang="less">
.fixed {
	position: fixed;
}
.todoList {
	.cu-item.finish {
		.liaohao {
			color: #aaaaaa;
			.cuIcon-formfill.text-blue {
				color: #aaaaaa;
			}
		}
	}
}
</style>
